package com.hyyt.shopjsp.logistics.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.logistics.pojo.ShopLogistics;
import com.hyyt.shopjsp.logistics.service.IShopLogisticsService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/20 0024下午 1:20.
 */

@Controller
@RequestMapping("/back/logistics")
public class ShopLogisticsController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopLogisticsController.class);
	private String basePath = "/back/logistics/";
	@Resource
	private IShopLogisticsService shopLogisticsService;
	
	/**
	 * 进入物流公司管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoLogisticsPage")
	public String gotoLogisticsPage(){
		return basePath+"logisticsList";
	}

	/**
	 * 查询所有物流公司
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="listLogistics",method=RequestMethod.POST)
	@ResponseBody
	public void listLogistics(String code, String deliveryCorpName,String isCommon, Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(code)){
			param.put("code",code.trim());
		}
		if (Utils.objectIsNotEmpty(deliveryCorpName)){
			param.put("deliveryCorpName",deliveryCorpName.trim());
		}
		if(Utils.objectIsNotEmpty(isCommon)){
		    param.put("isCommon",isCommon);
        }
		param.put("sortColumns", "sortCode asc");
		int totalRecordCount =shopLogisticsService.countShopLogistics(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopLogistics> list =shopLogisticsService.queryShopLogistics(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response,true);  //把内容发送到页面
	}

	/**
     * 根据id获得对象
     *
     * @param id
     */
	@RequestMapping(value = "getLogisticsObject", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getLogisticsObject(Integer id) {
		if (Utils.objectIsNotEmpty(id)) {
			ShopLogistics logistics = shopLogisticsService.loadShopLogistics(id);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(logistics));
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 保存或者修改物流信息
	 *
	 * @param logistics
	* @return
	*/
	@RequestMapping(value = "saveOrUpdateLogistics", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateLogistics(ShopLogistics logistics) {
		if (Utils.objectIsNotEmpty(logistics)) {
			if (Utils.objectIsEmpty(logistics.getLogisticsId())) {
				logistics.setCreateDate(new Timestamp(System.currentTimeMillis()));
				shopLogisticsService.saveShopLogistics(logistics);
			} else {
				shopLogisticsService.updateShopLogistics(logistics);
			}
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 根据传入的id删除物流信息
	 *
	 * @param ids
	 */
	@RequestMapping(value = "deleteLogistics", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteLogistics(String ids) {
		if (Utils.objectIsNotEmpty(ids)) {
			shopLogisticsService.deleteShopLogistics(Integer.parseInt(ids));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 初始化物流公司
	 * @param response
	 * @return
	 */
	@RequestMapping(value="initLogistics",method=RequestMethod.POST)
	public void initLogistics(HttpServletResponse response){
		Map<String, Object> params=new HashMap<>();
		params.put("sortColumns", "logisticsId desc");
		List<ShopLogistics> logisticsList = shopLogisticsService.queryShopLogistics(params);
		params.clear();
		params.put("logisticsList", logisticsList);
		ResultMsg.create(params, response);  //把内容发送到页面
	}
}
